package com.lagou.homework.part03

/**
 * @author: yehw
 * @date: 2020/10/9 21:56
 * @description: ⽤户位置时⻓统计
 */
object part03 {
  def main(args: Array[String]): Unit = {
    val p1 = new Person()
    p1.userId = "UserA"
    p1.locationId = "LocationA"
    p1.startTime = 8
    p1.stayTime = 60
    val p2 = new Person()
    p2.userId = "UserA"
    p2.locationId = "LocationA"
    p2.startTime = 9
    p2.stayTime = 60
    val p3 = new Person()
    p3.userId = "UserB"
    p3.locationId = "LocationB"
    p3.startTime = 10
    p3.stayTime = 60
    val p4 = new Person()
    p4.userId = "UserB"
    p4.locationId = "LocationB"
    p4.startTime = 11
    p4.stayTime = 60
    val person = combinedCalculation(p1, p2)
    val person1 = combinedCalculation(p3, p4)
    println(person)
    println(person1)
  }

  def combinedCalculation(p1: Person, p2: Person): Person = {
     var newPerson = new Person()
    if (p1.userId == p2.userId) {
      newPerson.userId=p1.userId
      newPerson.locationId=p1.locationId
      if (p1.startTime < p2.startTime) {
        newPerson.startTime = p1.startTime
      } else {
        newPerson.startTime = p2.startTime
      }
      newPerson.stayTime = p1.stayTime + p2.stayTime
      newPerson
    }
    newPerson
  }
}

//用户类
class Person {
  var userId: String = _
  var locationId: String = _
  var startTime: Int = _
  var stayTime: Int = _

  override def toString: String = {
    s"userId:$userId;locationId:$locationId;startTime:$startTime;stayTime:$stayTime"
  }
}